package com.prs.crm.service.stock.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.prs.crm.dao.stock.StockAlertDao;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.StockAlert;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StockAlertService;

public class StockAlertServiceImpl extends
		BaseServiceImpl<StockAlert, StockAlertDao> implements StockAlertService {

	@Override
	public Collection<StockAlert> getOnestockAlert(Product product,
			StoreRoom storeRoom) {
		return this.getDao().getOnestockAlert(product, storeRoom);

	}

	@Override
	public Collection<StockAlert> getStockAlertsByProduct(Product product) {
		return this.getDao().getStockAlertsByProduct(product);
	}

	@Override
	public void saveStockAlert(StockAlert[] stockAlerts, Product product,
			StoreRoom[] storerooms, String[] maxNumbers, String[] minNumbers) {
		
		//DecimalFormat    df   = new DecimalFormat("######0.00"); 
		for (int i = 0; i < maxNumbers.length; i++) {
			if (stockAlerts[i] == null) {
				if (Double.valueOf(maxNumbers[i]) != 0 && Double.valueOf(minNumbers[i]) >=0) {
					 
					StockAlert stockAlert = new StockAlert();
					stockAlert.setMaxNumber(Double.valueOf(maxNumbers[i]));
					stockAlert.setMinNumber(Double.valueOf(minNumbers[i]));
					stockAlert.setProduct(product);
					stockAlert.setStoreRoom(storerooms[i]);
					super.save(stockAlert);
				}
			} else {
				stockAlerts[i].setMaxNumber(Double.valueOf(maxNumbers[i]));
				stockAlerts[i].setMinNumber(Double.valueOf(minNumbers[i]));
				super.save(stockAlerts[i]);
			}
		}

	}

	@Override
	public Map<String, Double> validateOutStore(StoreRoom storeRoom,
			Product[] products, double[] integers,
			ProductStoreService productStoreService) {
	
		Map<String, Double> map = new HashMap<String, Double>();
	try {
		for (int i = 0; i < integers.length; i++) {
			//System.out.println(integers.length);
			StockAlert stockAlert = ((ArrayList<StockAlert>) this.getDao()
					.getOnestockAlert(products[i], storeRoom)).get(0);
			double count1 = productStoreService.getStoreNums(products[i],
					storeRoom);
			if (stockAlert.getMinNumber() > count1 - integers[i]) {
				map.put(products[i].getName(), stockAlert.getMinNumber()
						- (count1 - integers[i]));
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		return map;
	}

	@Override
	public Map<String, Double> validateInStore(StoreRoom storeRoom,
			Product[] products, double[] integers,
			ProductStoreService productStoreService) {
		Map<String, Double> map = new HashMap<String, Double>();
		try {
			for (int i = 0; i < integers.length; i++) {
				StockAlert stockAlert = ((ArrayList<StockAlert>) this.getDao()
						.getOnestockAlert(products[i], storeRoom)).get(0);
				double count1 = productStoreService.getStoreNums(products[i],
						storeRoom);
				
				if (stockAlert.getMaxNumber() < count1 + integers[i]) {

					map.put(products[i].getName(), (count1 + integers[i])
							- stockAlert.getMaxNumber());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	public List<String> validateStockAlert(StoreRoom storeRoom,
			Product[] products) {
		List<String> strings = new ArrayList<String>();
		try {
			for (int i = 0; i < products.length; i++) {
				ArrayList<StockAlert> stockAlerts = (ArrayList<StockAlert>) this
						.getDao().getOnestockAlert(products[i], storeRoom);
				if (stockAlerts.size() == 0) {
					strings.add(products[i].getName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strings;

	}

	@Override
	public Collection<StockAlert> getAllStockAlerts() {
		// TODO Auto-generated method stub
		return this.getDao().getAllStockAlerts();
	}

}
